package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.entity.AddressBook;
import com.example.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理簿
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
@Api(tags = "地址簿相关接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 保存地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressBook",value = "地址簿实体",required = true)
    })
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址信息")
    @PutMapping("/default")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressBook",value = "地址簿实体",required = true)
    })
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        //把所有的地址默认设置为0，清除之前设置的默认地址
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址系信息
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址系信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "地址簿id",required = true)
    })
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id){
        AddressBook addressBook=addressBookService.getById(id);
        if(addressBook==null){
            return R.error("没有该地址信息");
        }
        return R.success(addressBook);
    }

    /**
     * 获取默认地址信息
     * @return
     */
    @ApiOperation("获取默认地址信息")
    @GetMapping("default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook=addressBookService.getOne(queryWrapper);

        if(addressBook==null){
            return R.error("没有找到该信息");
        }else {
            return R.success(addressBook);
        }
    }

    /**
     * 获取地址信息列表
     * @param addressBook
     * @return
     */
    @ApiOperation("获取地址信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "地址簿id",required = true)
    })
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);

        return R.success(addressBookService.list(lambdaQueryWrapper));
    }
}

